package mvc;


import mvc.bezier.BezierModel;
import mvc.helix.HelixModel;
import mvc.selix.SelixModel;
import mvc.tubolar.TubolarModel;
import mvc.viviani.VivianiModel;

import org.geom.curve.Bezier;
import org.geom.curve.Helix;
import org.geom.curve.SElix;
import org.geom.curve.Viviani;
import org.geom.surface.TubolarSurface;

public class CurveModelManager {

	//-------------------------------------------------------- curve type
	public static final int SELI = 0;
	public static final int BEZIER = 1;
	public static final int HELIX = 2;
	public static final int VIVIANI = 3;
	
	//-------------------------------------------------------- classvars
	//-------------------------------------------------------- curves instances	
	
	// trunk
	private CurveModel currentTrunk = null;
	
	// strange helix
	private SelixModel seliModel = null;
	
	// tubolar surface
	private TubolarModel tubeModel = null;
	
	//possibles trunks
	private BezierModel bezierModel = null;
	private HelixModel helixModel = null;
	private VivianiModel vivianiModel = null;
	
	//-------------------------------------------------------- singleton
	private static CurveModelManager instance = null;
	private CurveModelManager() { 
	
	}
	public static CurveModelManager getInstance() {
		if(instance==null) instance = new CurveModelManager();
		return instance;
	}

	
	//-------------------------------------------------------- curve builder
	//-------------------------------------------------------- publics
	public SelixModel getSelix() {
		if(currentTrunk==null) getBezier(); // default trunk is bezier
		if(seliModel==null){
			SElix s = new SElix(currentTrunk);
			seliModel = new SelixModel(s);
		}	
		else setTrunk(currentTrunk);
		return seliModel;
	}
	
	//---------------------------------------------- trunks factory
	public BezierModel getBezier() {
		if(bezierModel==null) {
			Bezier bezierCurve = new Bezier(6);
			bezierModel = (BezierModel) new BezierModel(bezierCurve);
		}
		setTrunk(bezierModel);
		return bezierModel;
	}

	public HelixModel getHelix() {
		if(helixModel==null) {
			Helix helixCurve = new Helix();
			helixModel = new HelixModel(helixCurve);
		}
		setTrunk(helixModel);
		return helixModel;
	}
	
	public VivianiModel getViviani() {
		if(vivianiModel==null) {
			Viviani vivianiCurve = new Viviani();
			vivianiModel = new VivianiModel(vivianiCurve);
		}
		setTrunk(vivianiModel);
		return vivianiModel;
	}

	//-------------------------------------------------------- switch trunk logics	
	
	
	private void setTrunk(CurveModel model) {
		currentTrunk = model;
		model.addParent(seliModel);
		if(seliModel!=null)
			seliModel.setTrunk(currentTrunk);
	}
	
	//-------------------------------------------------------- tubolar 
	public TubolarModel getSeliTubolar() {
		System.out.println("CurveManager > getSeliTubolar");
	
		if(tubeModel==null) {
			TubolarSurface tube = new TubolarSurface(seliModel);
			tubeModel = new TubolarModel(tube);
		}
		getSelix().addParent(tubeModel);
		return tubeModel;
	}
	public void setAllDirty() {
		if(seliModel!=null)seliModel.dirty = true;
		if(bezierModel!=null)bezierModel.dirty = true;
		if(helixModel!=null)helixModel.dirty = true;
		if(tubeModel!=null)tubeModel.dirty = true;
		
	}
	public CurveModel getCurrentTrunkModel() {
		return this.currentTrunk;
	}
	
}
